package com.example.demo.controller;


import com.example.comon.domain.LoginUser;
import com.example.comon.domain.R;
import com.example.comon.utils.UserUtil;
import com.example.demo.domain.Bo.UserListBo;
import com.example.demo.domain.Menu;
import com.example.demo.domain.Vo.userListVo;
import com.example.demo.domain.Bo.addUserBo;
import com.example.demo.domain.User;
import com.example.demo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/test")
@Api(value = "test",tags = "测试")
public class TestController {

    //private List<User> list = new ArrayList<>();//自定义一个存储容器来保存 。保存到内存，未持久保存（也就是磁盘里面）。

    @Autowired
    private IUserService userService;

    @GetMapping("/getUser")
    @ApiOperation("获取当前登录用户")
    public R<User> getUser(){
        LoginUser loginUser = UserUtil.getLoginUser();
        Integer userId = loginUser.getUserId();
        User user = userService.selectByUserId(userId);
        return R.success(user);
    }

    @GetMapping("/getUserList")
    @ApiOperation("获取用户列表")
    public R<List<userListVo>> getUserList(UserListBo bo){
        User user = new User();
        user.setAccount(bo.getAccount());
        user.setName(bo.getName());
        List<userListVo> list =  userService.selectUser(user);
        //LoginUser loginUser = UserUtil.getLoginUser();
        //System.out.println("========="+loginUser.getUserId());
        return R.success(list);
    }



    @PostMapping("/addUser")
    @ApiOperation("增加用户")
    public R<User> addUser(@RequestBody addUserBo bo){
        User user = new User();
        user.setAccount(bo.getAccount());
        user.setPassword(bo.getPassword());
        user.setCreate_by(1111);
        user.setCreate_time(new Date());
        user.setName(bo.getName());
        user.setUpdate_by(bo.getUpdate_by());
        user.setUpdate_time(bo.getUpdate_time());
        userService.insertUser(user);
        return R.success(user);
    }

    @PostMapping("/updateUserById")
    @ApiOperation("修改用户")
    public R updateUserById(@RequestBody User user){
        userService.updateUserById(user);
        return R.success();
    }

    @DeleteMapping("/delUserById")
    @ApiOperation("删除用户")
    public R delUserById(Integer id){
        userService.delUserById(id);
        return R.success();
    }

    @GetMapping("/getMenuByUserId")
    @ApiOperation("查找用户有那些菜单")
    public  R getMenuByUserId(){
        LoginUser loginUser = UserUtil.getLoginUser();
        Integer userId = loginUser.getUserId();
        List<Menu> list =userService.getMenuByUserId(userId);
        return R.success(list);
    }

}
